package com.tjsinfo.tjsoa.articles.bo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.jdbc.core.RowMapper;

import com.tjsinfo.tjsoa.Dao.IHQLDao;
import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.articles.vo.TIndexArticles;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.common.ToolKit;

public class TArticlesManageImpl implements ITArticlesManage{
	private IQbcDao qbcDao;
	private IHibernateDao hibernateDao;
	private IJDBCDao jdbcDao;
	private IHQLDao hqlDao;
//	private ISpringDao springDao;
	
	
//	public ISpringDao getSpringDao() {
//		return springDao;
//	}
//
//	public void setSpringDao(ISpringDao springDao) {
//		this.springDao = springDao;
//	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public IJDBCDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(IJDBCDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public IHQLDao getHqlDao() {
		return hqlDao;
	}

	public void setHqlDao(IHQLDao hqlDao) {
		this.hqlDao = hqlDao;
	}

	/**
	 * 根据cid查找文章列表
	 * @param cid
	 * @return 返回符合条件的分页显示文章列表
	 */	
	public PaginationSupport findByCid(Integer cid, String sstart,String slimit) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class); 
		criteria.add(Restrictions.eq("cid", cid));
		List<Order> orderlist = new ArrayList<Order>();
		if(cid == 164){
			orderlist.add(Order.asc("showOrder"));
		}else{
			orderlist.add(Order.desc("showOrder"));
		}
		orderlist.add(Order.desc("id"));
		PaginationSupport ps= qbcDao.findPageByCriteria(criteria,orderlist,Integer.parseInt(slimit), Integer.parseInt(sstart));
		return ps;
	}
	
	public PaginationSupport findByCid(Integer cid, String sstart,String slimit,Date date) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class); 
		criteria.add(Restrictions.eq("cid", cid));
		criteria.add(Restrictions.sqlRestriction("CAST({alias}.availableDate AS DateTime)>=?",date,Hibernate.DATE));
		List<Order> orderlist = new ArrayList<Order>();
		orderlist.add(Order.desc("showOrder"));
		orderlist.add(Order.desc("id"));
		PaginationSupport ps= qbcDao.findPageByCriteria(criteria,orderlist,Integer.parseInt(slimit), Integer.parseInt(sstart));
		return ps;
	}
	
	
	public TIndexArticles findByID(String articles_uuid) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class); 
		criteria.add(Restrictions.eq("uuid", articles_uuid));
		return  (TIndexArticles) qbcDao.getCriteria(criteria).iterator().next();
	}
	
	public void deleteArticles(String articles_uuid) {
		// TODO Auto-generated method stub
		Integer articles_id= Integer.parseInt(articles_uuid);
		TIndexArticles articles = (TIndexArticles) hibernateDao.findById(TIndexArticles.class, articles_id);
		hibernateDao.delete(articles);
	}
	
	public void save(TIndexArticles articles) {
		// TODO Auto-generated method stub
		hibernateDao.saveObject(articles);
	}
	
	public void update(TIndexArticles articles) {
		// TODO Auto-generated method stub
		hibernateDao.update(articles);
	}

/**
 * 根据strsearch的值对所有文章的title进行模糊查询
 */	
	public PaginationSupport findByTitle(String strsearch,String sstart,String slimit) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class); 
		criteria.add(Restrictions.like("title", "%"+strsearch+"%"));
		List<Order> orderlist = new ArrayList<Order>();
		orderlist.add(Order.desc("showOrder"));
		orderlist.add(Order.desc("id"));
		PaginationSupport ps = qbcDao.findPageByCriteria(criteria,orderlist,Integer.parseInt(slimit), Integer.parseInt(sstart));
		return ps;
	}

	/**
	 * 根据cid及检索条件strsearch查找文章列表
	 * @param cid
	 * @return 返回符合条件的分页显示文章列表
	 */
public PaginationSupport findByCidSearch(Integer cid, String strsearch,
		String sstart, String slimit) {
	// TODO Auto-generated method stub
	DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class); 
//	criteria.add(Restrictions.eq("cid", cid));
//	if (strsearch.length()>0){
//		criteria.add(Restrictions.or(Restrictions.like("title", "%"+strsearch+"%"),
//					Restrictions.or(Restrictions.like("keyWord", "%"+strsearch+"%"),
//							Restrictions.like("smallContent", "%"+strsearch+"%"))));
//	}
//	List<Order> orderlist = new ArrayList<Order>();
//	orderlist.add(Order.desc("showOrder"));
//	orderlist.add(Order.desc("id"));
//	PaginationSupport ps= qbcDao.findPageByCriteria(criteria,orderlist,Integer.parseInt(slimit), Integer.parseInt(sstart));
	return findByCidSearchcommon(criteria, cid, strsearch, sstart, slimit);
}

/**
 * 根据cid、userId及检索条件strsearch查找文章列表
 * @param cid
 * @param userId
 * @return 返回符合条件的分页显示文章列表
 */
public PaginationSupport findByCidSearch(Integer cid,Integer userId,
		String strsearch, String sstart, String slimit) {
	// TODO Auto-generated method stub
	DetachedCriteria criteria = DetachedCriteria.forClass(TIndexArticles.class); 
	criteria.add(Restrictions.eq("cid", cid));
	criteria.add(Restrictions.eq("userId", userId));	
	
	if (strsearch.length()>0){
		criteria.add(Restrictions.or(Restrictions.like("title", "%"+strsearch+"%"),
					Restrictions.or(Restrictions.like("keyWord", "%"+strsearch+"%"),
							Restrictions.like("smallContent", "%"+strsearch+"%"))));
	}
	List<Order> orderlist = new ArrayList<Order>();
	if(cid == 164){
		orderlist.add(Order.asc("showOrder"));
	}else{
		orderlist.add(Order.desc("showOrder"));
	}
	orderlist.add(Order.desc("id"));
	PaginationSupport ps=qbcDao.findPageByCriteria(criteria,orderlist,Integer.parseInt(slimit), Integer.parseInt(sstart));
//	return findByCidSearchcommon(criteria, cid, strsearch, sstart, slimit);
	return ps;
}
/**
 * 根据cid、检索条件strsearch查找文章列表
 * @param criteria 传入的DetachedCriteria对象
 * @param cid
 * @param strsearch
 * @param sstart开始记录数
 * @param slimit返回记录数
 * @return
 */
private PaginationSupport findByCidSearchcommon(DetachedCriteria criteria,Integer cid, String strsearch,
		String sstart, String slimit){
	
		criteria.add(Restrictions.eq("cid", cid));
		if (strsearch.length()>0){
			criteria.add(Restrictions.or(Restrictions.like("title", "%"+strsearch+"%"),
						Restrictions.or(Restrictions.like("keyWord", "%"+strsearch+"%"),
								Restrictions.like("smallContent", "%"+strsearch+"%"))));
		}
		List<Order> orderlist = new ArrayList<Order>();
		if(cid == 164){
			orderlist.add(Order.asc("showOrder"));
		}else{
			orderlist.add(Order.desc("showOrder"));
		}		orderlist.add(Order.desc("id"));
//		String sql="with showorder as ( select top 100   * from dbo.t_index_articles where cid='"+cid+"' and showOrder != '' order by showOrder asc),"
//                  +" createDate as (select top 100  * from dbo.t_index_articles where cid='"+cid+"' and showOrder = '' order by createDate desc)"
//                  +" select * from showorder union all select * from createDate";	
		//List list = hqlDao.executeList(sql);
		PaginationSupport ps=qbcDao.findPageByCriteria(criteria,orderlist,Integer.parseInt(slimit), Integer.parseInt(sstart));
		//PaginationSupport ps=springDao.buildSQLResultSet(sql, new String[]{"title"}, 20, Integer.parseInt(sstart), 200);
		return ps;
}

/**
 * 将流程中产生的公文发布到新闻模块 
 * @param title 文章标题
 * @param url 打开链接
 * @param cid 栏目id
 * @param currentuser 当前用户 保存于session中的User类型
 */
public void publishDoc(String title,String url,Integer cid,User currentuser)
{
    //获取uuid
	UUID uuid = UUID.randomUUID();
	String uid = uuid.toString().replace("-", "");
	//获取当前日期作为建立日期
	java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd");
	String strdate=format1.format(new Date());
	
	TIndexArticles articletmp= new TIndexArticles();
	articletmp.setTitle(title);
	articletmp.setCid(cid);
	articletmp.setUrl(url);
	articletmp.setUserId(currentuser.getUser_id());
	articletmp.setUserName(currentuser.getUsername());
	articletmp.setUuid(uid);
	articletmp.setCreateDate(strdate);
	//articletmp.setShowOrder();
	articletmp.setAuthor("");
	articletmp.setShortTitle("");
	hibernateDao.saveObject(articletmp);
}

}

